//
//  BaiKeSecondClassesPopView.m
//  BaseProject
//
//  Created by mac on 2020/12/22.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BaiKeSecondClassesPopView.h"
#import "BaiKeMainClassesModel.h"
@interface BaiKeSecondClassesPopView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)UIButton *deleteButton;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)NSMutableArray<UILabel *> *itemsArray;
@property(nonatomic,strong)UIView *removeView;
@property(nonatomic,strong)UILabel *preLab;
//右侧的视图
@property(nonatomic,strong)UIScrollView *rightView;
@property(nonatomic,strong)NSMutableArray<UILabel *> *itemsArray_right;
@property(nonatomic,strong)UIView *removeView_right;
@end

@implementation BaiKeSecondClassesPopView
- (instancetype)initWithFrame:(CGRect)frame withParentView:(UIView *)parentView
{
    if (self = [super initWithFrame:frame withParentView:parentView]) {
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    
}
- (void)createUI
{
    self.titleLab.text = @"分类选择";
    [self deleteButton];
    [self lineView];
    self.mainView.frame = CGRectMake(0, self.lineView.bottom, 152*ADAPTER_WIDTH, self.height - self.lineView.bottom - kBottomSafeHeight);
    self.rightView.left = 152*ADAPTER_WIDTH;
    self.rightView.width = self.width - 152*ADAPTER_WIDTH;
}
- (void)setClassesModel:(BaiKeMainClassesModel *)classesModel
{
    _classesModel = classesModel;
    [self createLeftMainView];
}
- (void)createLeftMainView
{
    [self removeView];
    CGFloat maxWidth = self.mainView.width - 23*ADAPTER_WIDTH,maxHeight = 38*ADAPTER_WIDTH,orinX = 18*ADAPTER_WIDTH;
    for (NSInteger index = 0; index < self.classesModel.data.count; index ++) {
        BaiKeMainClassesDataModel *listModel = self.classesModel.data[index];
        UILabel *lab = [[UILabel alloc]init];
        [self.mainView addSubview:lab];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.font = kFontRegular14;
        lab.numberOfLines = 1;
        CGFloat itemWidth = [listModel.twoName sizeWithFont:lab.font].width;
        lab.frame = CGRectMake(orinX, index * maxHeight, maxWidth, maxHeight);
        lab.text = listModel.twoName;
        
        [self.itemsArray addObject:lab];
        if (itemWidth > maxWidth) {
            lab.numberOfLines = 2;
            lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        }
        
        lab.tag = index;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickItemLab:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
        
        if (listModel.isSelected) {
            lab.textColor = rgba(255, 66, 66, 1);
            self.removeView.top = lab.top;
            self.preLab = lab;
            [self createRightView];
        }
        
        self.mainView.contentSize = CGSizeMake(0, MAX(self.mainView.height + 1, lab.bottom));
    }
    
    [self adjustOffsetY];
}
- (void)createRightView
{
    NSLog(@"createRightView");
    [self.itemsArray_right enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.itemsArray_right removeAllObjects];
    [self removeView_right];
    UILabel *preLab;
    CGFloat maxWidth = self.rightView.width - 37*ADAPTER_WIDTH,maxHeight = 38*ADAPTER_WIDTH,orinX = 27*ADAPTER_WIDTH;
    for (NSInteger index = 0; index < self.classesModel.data[self.preLab.tag].sysClassificationList.count; index ++) {
        BaiKeMainClassesDataModel *listModel = self.classesModel.data[self.preLab.tag].sysClassificationList[index];
        UILabel *lab = [[UILabel alloc]init];
        [self.rightView addSubview:lab];
        lab.textColor = kColor102;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.font = kFontRegular14;
        lab.numberOfLines = 1;
        CGFloat itemWidth = [listModel.threeName sizeWithFont:lab.font].width;
        lab.frame = CGRectMake(orinX, index * maxHeight, maxWidth, maxHeight);
        lab.text = listModel.threeName;
        
        [self.itemsArray_right addObject:lab];
        if (itemWidth > maxWidth) {
            lab.numberOfLines = 2;
            lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        }
        
        lab.tag = index;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickRightItemLab:)];
        lab.userInteractionEnabled = YES;
        [lab addGestureRecognizer:tap];
        
        
        self.rightView.contentSize = CGSizeMake(0, MAX(self.rightView.height + 1, lab.bottom));
        
        if (listModel.isSelected) {
            preLab = lab;
            lab.textColor = rgba(255, 66, 66, 1);
            self.removeView_right.top = lab.top;
        }
    }
    
    
    CGFloat offsetY = preLab.centerY - self.rightView.height * 0.5;
    if (offsetY < 0) {
        offsetY = 0;
    }
    if (self.rightView.contentSize.height > self.rightView.height) {
        CGFloat maxOffsetY = self.rightView.contentSize.height - self.rightView.height;
        if (offsetY > maxOffsetY) {
            offsetY = maxOffsetY;
        }
    }
    [self.rightView setContentOffset:CGPointMake(0, offsetY) animated:YES];
}
#pragma mark - 调整位移
- (void)adjustOffsetY
{
    CGFloat offsetY = self.preLab.centerY - self.mainView.height * 0.5;
    if (offsetY < 0) {
        offsetY = 0;
    }
    if (self.mainView.contentSize.height > self.mainView.height) {
        CGFloat maxOffsetY = self.mainView.contentSize.height - self.mainView.height;
        if (offsetY > maxOffsetY) {
            offsetY = maxOffsetY;
        }
    }
    [self.mainView setContentOffset:CGPointMake(0, offsetY) animated:YES];
}
#pragma mark - click
- (void)clickDeleteButton:sender
{
    [self dismiss];
}
- (void)clickItemLab:(UITapGestureRecognizer *)sender
{
    UILabel *clickLab = (UILabel *)sender.view;
    if (self.preLab == clickLab) {
        return;
    }
    self.classesModel.data[self.preLab.tag].isSelected = NO;
    self.preLab.textColor = kColor51;
    self.preLab = clickLab;
    self.preLab.textColor = rgba(255, 66, 66, 1);
    self.classesModel.data[self.preLab.tag].isSelected = YES;
    WK(weakSelf)
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.removeView.top = weakSelf.preLab.top;
    }];
    [self createRightView];
    
    [self adjustOffsetY];
}
- (void)clickRightItemLab:(UITapGestureRecognizer *)sender
{
    //整理数据 选中
    for (NSInteger index = 0; index < self.classesModel.data[self.preLab.tag].sysClassificationList.count; index ++) {
        BaiKeMainClassesDataModel *listModel = self.classesModel.data[self.preLab.tag].sysClassificationList[index];
        if (sender.view.tag == index) {
            listModel.isSelected = YES;
        } else {
            listModel.isSelected = NO;
        }
    }
    !self.callBackSelectedBlock ?: self.callBackSelectedBlock (self.preLab.tag,sender.view.tag);
    //整理数据 选中
    [self dismiss];
}
#pragma mark - UISCrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView == self.rightView) {
        self.pan.enabled = NO;
    } else {
        self.pan.enabled = YES;
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView == self.rightView) {
        self.pan.enabled = YES;
    }
}
#pragma mark - lazy懒加载
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightMedium];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(18*ADAPTER_WIDTH, 16*ADAPTER_WIDTH, 200*ADAPTER_WIDTH, 22*ADAPTER_WIDTH);
        _titleLab = lab;
    }
    return _titleLab;
}
- (UIButton *)deleteButton
{
    if (!_deleteButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setImage:[UIImage getPNGimageInBundleWithName:@"BaiKe_delete"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(self.width - 54*ADAPTER_WIDTH, 0, 54*ADAPTER_WIDTH, 34*ADAPTER_WIDTH);//14x14
        button.imageEdgeInsets = UIEdgeInsetsMake(10*ADAPTER_WIDTH, 20*ADAPTER_WIDTH, 10*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
        button.centerY = self.titleLab.centerY;
        _deleteButton = button;
    }
    return _deleteButton;
}
- (UIView *)lineView
{
    if (!_lineView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kLineColor;
        view.frame = CGRectMake(0, 55*ADAPTER_WIDTH, self.width, kLineHeight);
        [self addSubview:view];
        _lineView = view;
    }
    return _lineView;
}
- (NSMutableArray<UILabel *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
- (NSMutableArray<UILabel *> *)itemsArray_right
{
    if (!_itemsArray_right) {
        _itemsArray_right = [NSMutableArray array];
    }
    return _itemsArray_right;
}
- (UIScrollView *)rightView
{
    if (!_rightView) {
        UIScrollView *view = [[UIScrollView alloc]initWithFrame:CGRectMake(self.mainView.left, self.mainView.top, self.width - self.mainView.width, self.mainView.height)];
        [self addSubview:view];
        view.backgroundColor = kBGDarkColor;
        view.delegate = self;
        _rightView = view;
    }
    return _rightView;
}
- (UIView *)removeView
{
    if (!_removeView) {
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(0, 0, self.mainView.width, 38*ADAPTER_WIDTH);
        view.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:view.size andWithColors:@[(__bridge id)rgba(255, 236, 236, 1).CGColor,(__bridge id)rgba(255, 255, 255, 1).CGColor]];
        [self.mainView addSubview:view];
        
        UIImageView *iv = [[UIImageView alloc]init];
        [view addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"BaiKe_doline"];
        iv.frame = CGRectMake(0, (view.height - 19*ADAPTER_WIDTH) * 0.5, 4*ADAPTER_WIDTH, 19*ADAPTER_WIDTH);
        _removeView = view;
    }
    return _removeView;
}
- (UIView *)removeView_right
{
    if (!_removeView_right) {
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(0, 0, self.rightView.width, 38*ADAPTER_WIDTH);
        view.backgroundColor = [UIColor ColorGradientType:ColorGradientTypeHorizontal andWithSize:view.size andWithColors:@[(__bridge id)rgba(255, 236, 236, 1).CGColor,(__bridge id)rgba(255, 255, 255, 1).CGColor]];
        [self.rightView addSubview:view];
        
        UIImageView *iv = [[UIImageView alloc]init];
        [view addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"BaiKe_doline"];
        iv.frame = CGRectMake(0, (view.height - 19*ADAPTER_WIDTH) * 0.5, 4*ADAPTER_WIDTH, 19*ADAPTER_WIDTH);
        _removeView_right = view;
    }
    return _removeView_right;
}
@end
